package com.ithiema.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ithiema.reggie.Service.EmployeeService;
import com.ithiema.reggie.common.R;
import com.ithiema.reggie.entity.Employee;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

//Employee控制层  (员工)

@Slf4j//日志
@RestController
@RequestMapping("/employee")
//还需要在引导类上加一个注解@ServletComponentScan
// 扫描过滤器配置的@WebFilter注解， 扫描上之后， 过滤器在运行时就生效了。
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //登录 .
    @PostMapping("/login")
    private R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){

// 将页面提交的密码password进行md5加密处理, 得到加密后的字符串(string 类型)
        String password = employee.getPassword();
        String s = DigestUtils.md5DigestAsHex(password.getBytes());

// 根据页面提交的用户名username查询数据库中员工数据信息
        LambdaQueryWrapper<Employee> lwp = new LambdaQueryWrapper<>();
        System.err.println(employee.getUsername());
        //获取用户输入的密码
        lwp.eq(Employee::getUsername,employee.getUsername());//
        Employee one = employeeService.getOne(lwp);//账户和密码都查询到了

// 如果没有查询到, 则返回登录失败结果
        if (one==null){
            return R.error("登录失败");
        }

// 密码比对，如果不一致, 则返回登录失败结果
        if (!one.getPassword().equals(s)){
            return R.error("登录失败");
        }

// 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (one.getStatus()==0){
            return R.error("账号已禁用");
        }

// 登录成功，将员工id存入Session, 并返回登录成功结果
        request.getSession().setAttribute("employee",one.getId());

        return R.success(one);
    }

    //退出登录
    @PostMapping("/logout")
    private R  logout(HttpServletRequest request){
//清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    //添加{新增员工}
    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpServletRequest request){

        log.info("新增员工,员工信息:{}",employee);

        //设置初始密码123456，需要进行md5加密处理  设置用set 获取用get
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);
        return R.success("新增员工成功");
    }


    //分页查询    page 当前查询页码    pageSize 每页展示记录数     name 员工姓名 - 模糊查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize , String name){
        log.info("接收分页查询员工的请求:page:{},pageSize:{},name:{}",page,pageSize,name);
        //分页条件构建器

        Page  pageInfo=new Page(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        //添加排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询

        employeeService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }


    //修改
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpServletRequest request){
        log.info(employee.toString());
        employeeService.updateById(employee);

        return R.success("员工信息修改成功");
        //前段出现  损失精度
    }

    //编辑
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable long id){
        log.info("根据id查询员工信息");
        Employee byId = employeeService.getById(id);
        if(byId != null){
            return R.success(byId);
        }
        return R.error("没有查询到对应员工信息");
    }
}
